//
//  UIView+CYViewExtension.m
//  SafetyInHand
//
//  Created by admin on 2022/7/21.
//
#define RGBA_COLOR(r,g,b,a)  [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

#import "UIView+CYViewExtension.h"

#import <objc/runtime.h>

NSTimeInterval const animateDuration = 0.25;

typedef void(^VoidBlock)();

@interface UIView ()
@property (nonatomic, copy) VoidBlock coverViewTouchBlock;
@end

@implementation UIView (ZZViewExtension)

#pragma mark - X

- (void)cy_setX:(CGFloat)x {
    [self cy_setX:x withAnimate:NO];
}

- (CGFloat)cy_x {
    return self.frame.origin.x;
}

- (CGRect)cy_newFrameWithX:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    return frame;
}

- (void)cy_setX:(CGFloat)x withAnimate:(BOOL)animate {
    [self cy_setX:x withAnimate:animate completion:nil];
}

- (void)cy_setX:(CGFloat)x withAnimate:(BOOL)animate completion:(void (^)(BOOL))completion {
    if (animate) {
        [self cy_setX:x withAnimate:animate animateDuration:animateDuration completion:completion];
    } else {
        self.frame = [self cy_newFrameWithX:x];
        if (completion) {
            completion(YES);
        }
    }
}

- (void)cy_setX:(CGFloat)x withAnimate:(BOOL)animate animateDuration:(NSTimeInterval)duration completion:(void (^)(BOOL))completion {
    CGRect frame = [self cy_newFrameWithX:x];
    [UIView animateWithDuration:duration animations:^{
        self.frame = frame;
    }                completion:completion];
}

#pragma mark - Y

- (void)cy_setY:(CGFloat)y {
    [self cy_setY:y withAnimate:NO];
}

- (CGFloat)y {
    return self.frame.origin.y;
}

- (CGRect)cy_newFrameWithY:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    return frame;
}

- (void)cy_setY:(CGFloat)y withAnimate:(BOOL)animate {
    [self cy_setY:y withAnimate:animate completion:nil];
}

- (void)cy_setY:(CGFloat)y withAnimate:(BOOL)animate completion:(void (^)(BOOL))completion {
    if (animate) {
        [self cy_setY:y withAnimate:animate animateDuration:animateDuration completion:completion];
    } else {
        self.frame = [self cy_newFrameWithY:y];
        if (completion) {
            completion(YES);
        }
    }
}

- (void)cy_setY:(CGFloat)y withAnimate:(BOOL)animate animateDuration:(NSTimeInterval)duration completion:(void (^)(BOOL))completion {
    CGRect frame = [self cy_newFrameWithY:y];
    [UIView animateWithDuration:duration animations:^{
        self.frame = frame;
    }                completion:completion];

}

#pragma mark - Width

- (void)cy_setWidth:(CGFloat)width {
    [self cy_setWidth:width withAnimate:NO];
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (CGRect)cy_newFrameWithWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    return frame;
}

- (void)cy_setWidth:(CGFloat)width withAnimate:(BOOL)animate {
    [self cy_setWidth:width withAnimate:animate completion:nil];
}

- (void)cy_setWidth:(CGFloat)width withAnimate:(BOOL)animate completion:(void (^)(BOOL))completion {
    if (animate) {
        [self cy_setWidth:width withAnimate:animate animateDuration:animateDuration completion:completion];
    } else {
        self.frame = [self cy_newFrameWithWidth:width];
        if (completion) {
            completion(YES);
        }
    }
}

- (void)cy_setWidth:(CGFloat)width withAnimate:(BOOL)animate animateDuration:(NSTimeInterval)duration completion:(void (^)(BOOL))completion {
    CGRect frame = [self cy_newFrameWithWidth:width];
    [UIView animateWithDuration:duration animations:^{
        self.frame = frame;
    }                completion:completion];
}

#pragma mark - Height

- (void)cy_setHeight:(CGFloat)height {
    [self cy_setHeight:height withAnimate:NO];
}

- (CGFloat)cy_height {
    return self.frame.size.height;
}

- (CGRect)cy_newFrameWithHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    return frame;
}

- (void)cy_setHeight:(CGFloat)height withAnimate:(BOOL)animate {
    [self cy_setHeight:height withAnimate:animate completion:nil];
}

- (void)cy_setHeight:(CGFloat)height withAnimate:(BOOL)animate completion:(void (^)(BOOL))completion {
    if (animate) {
        [self cy_setHeight:height withAnimate:animate animateDuration:animateDuration completion:completion];
    } else {
        self.frame = [self cy_newFrameWithHeight:height];
        if (completion) {
            completion(YES);
        }
    }
}

- (void)cy_setHeight:(CGFloat)height withAnimate:(BOOL)animate animateDuration:(NSTimeInterval)duration completion:(void (^)(BOOL))completion {
    CGRect frame = [self cy_newFrameWithHeight:height];
    [UIView animateWithDuration:duration animations:^{
        self.frame = frame;
    }                completion:completion];
}

#pragma mark - CenterX

- (void)cy_setCenterX:(CGFloat)centerX {
    [self cy_setCenterX:centerX withAnimate:NO];
}

- (CGFloat)cy_centerX {
    return self.center.x;
}

- (CGPoint)cy_newCenterWithCenterX:(CGFloat)centerX {
    CGPoint center = self.center;
    center.x = centerX;
    return center;
}

- (void)cy_setCenterX:(CGFloat)centerX withAnimate:(BOOL)animate {
    [self cy_setCenterX:centerX withAnimate:animate completion:nil];
}

- (void)cy_setCenterX:(CGFloat)centerX withAnimate:(BOOL)animate completion:(void (^)(BOOL))completion {
    if (animate) {
        [self cy_setCenterX:centerX withAnimate:animate animateDuration:animateDuration completion:completion];
    } else {
        self.center = [self cy_newCenterWithCenterX:centerX];
        if (completion) {
            completion(YES);
        }
    }
}

- (void)cy_setCenterX:(CGFloat)centerX withAnimate:(BOOL)animate animateDuration:(NSTimeInterval)duration completion:(void (^)(BOOL))completion {
    CGPoint center = [self cy_newCenterWithCenterX:centerX];
    [UIView animateWithDuration:duration animations:^{
        self.center = center;
    }                completion:completion];
}

#pragma mark - CenterY

- (void)cy_setCenterY:(CGFloat)centerY {
    [self cy_setCenterY:centerY withAnimate:NO];
}

- (CGFloat)cy_centerY {
    return self.center.y;
}

- (CGPoint)cy_newCenterWithCenterY:(CGFloat)centerY {
    CGPoint center = self.center;
    center.y = centerY;
    return center;
}

- (void)cy_setCenterY:(CGFloat)centerY withAnimate:(BOOL)animate {
    [self cy_setCenterY:centerY withAnimate:animate completion:nil];
}

- (void)cy_setCenterY:(CGFloat)centerY withAnimate:(BOOL)animate completion:(void (^)(BOOL))completion {
    if (animate) {
        [self cy_setCenterY:centerY withAnimate:animate animateDuration:animateDuration completion:completion];
    } else {
        self.center = [self cy_newCenterWithCenterY:centerY];
        if (completion) {
            completion(YES);
        }
    }
}

- (void)cy_setCenterY:(CGFloat)centerY withAnimate:(BOOL)animate animateDuration:(NSTimeInterval)duration completion:(void (^)(BOOL))completion {
    CGPoint center = [self cy_newCenterWithCenterY:centerY];
    [UIView animateWithDuration:duration animations:^{
        self.center = center;
    }                completion:completion];
}

#pragma mark - Top

- (void)cy_setTop:(CGFloat)top {
    [self cy_setY:top];
}

- (CGFloat)cy_top {
    return self.y;
}

- (void)cy_setTop:(CGFloat)top withAnimate:(BOOL)animate {
    [self cy_setY:top withAnimate:animate];
}

- (void)cy_setTop:(CGFloat)top withAnimate:(BOOL)animate completion:(void (^)(BOOL))completion {
    [self cy_setTop:top withAnimate:animate animateDuration:animateDuration completion:completion];
}

- (void)cy_setTop:(CGFloat)top withAnimate:(BOOL)animate animateDuration:(NSTimeInterval)duration completion:(void (^)(BOOL))completion {
    [self cy_setY:top withAnimate:animate animateDuration:duration completion:completion];
}

#pragma mark - Right

- (void)cy_setRight:(CGFloat)right {
    [self cy_setRight:right withAnimate:NO];
}

- (CGFloat)cy_right {
    return self.cy_x + self.width;
}

- (CGRect)cy_newFrameWithRight:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = right - self.width;
    return frame;
}

- (void)cy_setRight:(CGFloat)right withAnimate:(BOOL)animate {
    [self cy_setRight:right withAnimate:animate completion:nil];
}

- (void)cy_setRight:(CGFloat)right withAnimate:(BOOL)animate completion:(void (^)(BOOL))completion {
    if (animate) {
        [self cy_setRight:right withAnimate:animate animateDuration:animateDuration completion:completion];
    } else {
        self.frame = [self cy_newFrameWithRight:right];
        if (completion) {
            completion(YES);
        }
    }
}

- (void)cy_setRight:(CGFloat)right withAnimate:(BOOL)animate animateDuration:(NSTimeInterval)duration completion:(void (^)(BOOL))completion {
    CGRect frame = [self cy_newFrameWithRight:right];
    [UIView animateWithDuration:duration animations:^{
        self.frame = frame;
    }                completion:completion];
}

#pragma mark - Bottom

- (void)cy_setBottom:(CGFloat)bottom {
    [self cy_setBottom:bottom withAnimate:NO];
}

- (CGFloat)cy_bottom {
    return self.y + self.cy_height;
}

- (CGRect)cy_newFrameWithBottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = bottom - self.cy_height;;
    return frame;
}

- (void)cy_setBottom:(CGFloat)bottom withAnimate:(BOOL)animate {
    [self cy_setBottom:bottom withAnimate:animate completion:nil];
}

- (void)cy_setBottom:(CGFloat)bottom withAnimate:(BOOL)animate completion:(void (^)(BOOL))completion {
    if (animate) {
        [self cy_setBottom:bottom withAnimate:animate animateDuration:animateDuration completion:completion];
    } else {
        self.frame = [self cy_newFrameWithBottom:bottom];
        if (completion) {
            completion(YES);
        }
    }
}

- (void)cy_setBottom:(CGFloat)bottom withAnimate:(BOOL)animate animateDuration:(NSTimeInterval)duration completion:(void (^)(BOOL))completion {
    CGRect frame = [self cy_newFrameWithBottom:bottom];
    [UIView animateWithDuration:duration animations:^{
        self.frame = frame;
    }                completion:completion];
}

#pragma mark - Left

- (void)cy_setLeft:(CGFloat)left {
    [self cy_setX:left];
}

- (CGFloat)cy_left {
    return self.cy_x;
}

- (void)cy_setLeft:(CGFloat)left withAnimate:(BOOL)animate {
    [self cy_setX:left withAnimate:animate];
}

- (void)cy_setLeft:(CGFloat)left withAnimate:(BOOL)animate completion:(void (^)(BOOL))completion {
    [self cy_setLeft:left withAnimate:animate animateDuration:animateDuration completion:completion];
}

- (void)cy_setLeft:(CGFloat)left withAnimate:(BOOL)animate animateDuration:(NSTimeInterval)duration completion:(void (^)(BOOL))completion {
    [self cy_setX:left withAnimate:animate animateDuration:duration completion:completion];
}

#pragma mark - Size

- (CGSize)cy_size {
    return self.frame.size;
}

- (void)cy_setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

#pragma mark - Layer

- (void)cy_setLayerCornerRadius {
    [self cy_setLayerCornerRadiusWithRadius:self.cy_height / 2];
}

- (void)cy_setLayerCornerRadiusWithRadius:(CGFloat)radius {
    self.layer.cornerRadius = radius;
    self.clipsToBounds = YES;
}

- (void)cy_setBorderWidth:(CGFloat)width borderColor:(CGColorRef)borderColor {
    [self cy_setBorderWidth:width borderColor:borderColor radius:0];
}

- (void)cy_setBorderWidth:(CGFloat)width borderColor:(CGColorRef)borderColor radius:(CGFloat)radius {
    self.layer.borderWidth = width;
    self.layer.borderColor = borderColor;
    [self cy_setLayerCornerRadiusWithRadius:radius];
}

- (void)cy_shadowDefault {
    [self cy_setLayerShadow:[UIColor blackColor] shadowOffset:CGSizeMake(0.5, 0.5) shadowOpacity:0.4 shadowRadius:5.0 cornerRadius:5.0];
}

- (void)cy_setLayerShadow:(UIColor *)color shadowOffset:(CGSize)offset shadowOpacity:(CGFloat)opacity shadowRadius:(CGFloat)shadowRadius cornerRadius:(CGFloat)cornerRadius {
    self.layer.shadowColor = color.CGColor;
    self.layer.shadowOffset = offset;
    self.layer.shadowOpacity = opacity;
    self.layer.shadowRadius = shadowRadius;
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = NO;
}

- (void)cy_setViewRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:cornerRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

- (void)cy_setViewRoundingCorners:(UIRectCorner)corners cornerRadius:(CGFloat)radius {
    if (@available(iOS 11.0, *)) {
        self.layer.cornerRadius = radius;
        self.layer.maskedCorners = (CACornerMask) corners;
    } else {
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = path.CGPath;
        self.layer.mask = maskLayer;
    }
}


#pragma mark - UIWindow&UIScreen

+ (UIWindow *)cy_keyWindow {
    return [[UIApplication sharedApplication].delegate window];
}

+ (UIScreen *)cy_mainScreen {
    return [UIScreen mainScreen];
}

+ (CGRect)cy_mainScreenBounds {
    return self.cy_mainScreen.bounds;
}

#pragma mark - LoadFromNib

+ (id)cy_loadFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}

+ (NSString *)cy_ClassStr {
    return NSStringFromClass([self class]);
}

#pragma mark - RotateAnimation

- (void)cy_startRotationWithDisableViews:(NSArray *)disableViews {
    for (UIView *aView in disableViews) {
        aView.userInteractionEnabled = NO;
        aView.alpha = 0.7;
    }
    self.userInteractionEnabled = NO;
    [self cy_rotateAnimation];
}

- (void)cy_rotateAnimation {
    self.transform = CGAffineTransformRotate(self.transform, (M_2_PI / 180.0));
    [self performSelector:@selector(cy_rotateAnimation) withObject:nil afterDelay:0.01];
}

- (void)cy_stopRotationDisableViews:(NSArray *)disableViews {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(cy_rotateAnimation) object:nil];
    self.transform = CGAffineTransformIdentity;
    self.userInteractionEnabled = YES;
    for (UIView *aView in disableViews) {
        aView.userInteractionEnabled = YES;
        aView.alpha = 1;
    }
}

#pragma mark - UIGestureRecognizers

- (void)cy_removeAllGesture {
    NSArray *allGeses = [self gestureRecognizers];
    for (UIGestureRecognizer *ges in allGeses) {
        [self removeGestureRecognizer:ges];
    }
}

#pragma mark - ShowView

- (void)cy_showInViewWithZoom:(UIView *)aView {
    [aView addSubview:self];
    self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
    self.center = aView.center;
    self.alpha = 0;
    [UIView animateWithDuration:animateDuration animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
    }];
}

- (void)cy_removeWithZoomFromView:(UIView *)aView completion:(void (^)(BOOL finished))completion {
    [UIView animateWithDuration:animateDuration animations:^{
        self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
        self.alpha = 0;
    }                completion:^(BOOL finished) {
        if (completion) {
            completion(finished);
            [self removeFromSuperview];
        }
    }];
}

static char c_CoverviewTouchBlockKey;

+ (void)cy_setCoverViewTouchBlock:(void (^)())coverViewTouchBlock {
    objc_setAssociatedObject(self, &c_CoverviewTouchBlockKey, coverViewTouchBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

+ (VoidBlock)cy_coverViewTouchBlock {
    return objc_getAssociatedObject(self, &c_CoverviewTouchBlockKey);
}

+ (UIView *)cy_showCoverViewInSuperView:(UIView *)superView withCompletedBlock:(void (^)())completedblock {
    return [self cy_showCoverViewInSuperView:superView withCoverViewTouchBlock:nil completedBlock:completedblock];
}

+ (UIView *)cy_showCoverViewInSuperView:(UIView *)superView withCoverViewTouchBlock:(void (^)())touchBlock completedBlock:(void (^)())completedblock {
    return [self cy_showCoverViewInSuperView:superView frame:CGRectZero withCoverViewBgColor:[UIColor blackColor] alpha:0.4 coverViewTouchEnable:touchBlock != nil coverViewTouchBlock:touchBlock completedBlock:completedblock];
}

+ (UIView *)cy_showCoverViewInSuperView:(UIView *)superView frame:(CGRect)frame withCoverViewBgColor:(UIColor *)bgColor alpha:(CGFloat)alpha coverViewTouchEnable:(BOOL)enable coverViewTouchBlock:(void (^)())touchBlock completedBlock:(void (^)())completedBlock {
    CGRect coverFrame = frame;
    if (frame.size.width == 0 && frame.size.height == 0) {
        coverFrame = superView.bounds;
    }
    UIView *coverView = [[UIView alloc] initWithFrame:coverFrame];
    coverView.backgroundColor = [bgColor colorWithAlphaComponent:alpha];
    coverView.userInteractionEnabled = enable;

    if (enable && touchBlock) {
        [self cy_setCoverViewTouchBlock:touchBlock];
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cy_coverViewTap:)];
        [coverView addGestureRecognizer:tapGes];
    }
    [superView addSubview:coverView];
    if (completedBlock) {
        completedBlock();
    }
    return coverView;
}

- (void)cy_pushInView:(UIView *)aView completion:(void (^)(BOOL finished))completion {
    [aView addSubview:self];
    [self cy_setX:aView.width];
    [self cy_setCenterY:aView.cy_centerY];
    [self cy_setCenterX:aView.cy_centerX withAnimate:YES animateDuration:animateDuration completion:completion];
}

- (void)cy_popFromView:(UIView *)aView completion:(void (^)(BOOL finished))completion {
    [self cy_setX:aView.width withAnimate:YES animateDuration:animateDuration completion:completion];
}

- (void)cy_setAlpha:(CGFloat)alpha isAnimation:(BOOL)isAnimation duration:(NSTimeInterval)duration completionBlock:(void (^)())completion {
    if (isAnimation) {
        [UIView animateWithDuration:duration animations:^{
            [self setAlpha:alpha];
        }                completion:^(BOOL finished) {
            if (finished) {
                if (completion) {
                    completion();
                }
            }
        }];
    } else {
        [self setAlpha:alpha];
    }
}

- (void)cy_showZoomAnimationView {
    self.alpha = 0;
    CGFloat d1 = 0.2, d2 = 0.15;
    self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);

    __weak typeof(self) WS = self;
    [UIView animateWithDuration:d1 animations:^{
        WS.alpha = 1;
        WS.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
    }                completion:^(BOOL finished) {
        [UIView animateWithDuration:d2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            WS.alpha = 1;
            WS.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
        }                completion:^(BOOL finished2) {
        }];
    }];
}

- (void)cy_dismissViewCompletedBlock:(void (^)())completedBlock {
    CGFloat d1 = 0.2, d2 = 0.1;
    __weak typeof(self) WS = self;
    [UIView animateWithDuration:d2 animations:^{
        WS.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
    }                completion:^(BOOL finished) {
        [UIView animateWithDuration:d1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            WS.alpha = 0;
            WS.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);
        }                completion:^(BOOL finished2) {
            if (completedBlock) {
                completedBlock();
            }
        }];
    }];
}

#pragma mark - Selector

+ (void)cy_coverViewTap:(UITapGestureRecognizer *)tapGes {
    if (UIView.cy_coverViewTouchBlock) {
        UIView.cy_coverViewTouchBlock();
    }
}

#pragma mark - 绘制

/**
 ** lineView:       需要绘制成虚线的view
 ** lineLength:     虚线的宽度
 ** lineSpacing:    虚线的间距
 ** lineColor:      虚线的颜色
 **/
+ (CAShapeLayer *)cy_drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor {
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];

    //设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];

    //设置虚线宽度
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];

    //设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];

    //设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);

    [shapeLayer setPath:path];
    CGPathRelease(path);

    //把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
    return shapeLayer;
}

+ (CAShapeLayer *)cy_drawDashedBorderAroundView:(UIView *)lineView cornerRadius:(CGFloat)cornerRadius lineWidth:(CGFloat)lineWidth lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor {

    //drawing
    CGRect frame = lineView.bounds;

    CAShapeLayer *_shapeLayer = [CAShapeLayer layer];

    //creating a path
    CGMutablePathRef path = CGPathCreateMutable();

    //drawing a border around a view
    CGPathMoveToPoint(path, NULL, frame.size.width - cornerRadius * sin(M_PI_4 * 0.5), frame.size.height - cornerRadius * sin(M_PI_4 * 0.5));
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_4, M_PI_2, NO);
    CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height);
    CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO);
    CGPathAddLineToPoint(path, NULL, 0, cornerRadius);
    CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);
    CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO);
    CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius);
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_4, NO);

    //path is set as the _shapeLayer object's path
    _shapeLayer.path = path;
    CGPathRelease(path);

    _shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
    _shapeLayer.frame = frame;
    _shapeLayer.masksToBounds = NO;
    [_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
    _shapeLayer.fillColor = [[UIColor clearColor] CGColor];
    _shapeLayer.strokeColor = [lineColor CGColor];
    _shapeLayer.lineWidth = lineWidth;
    _shapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil];
    _shapeLayer.lineCap = kCALineCapRound;

    //_shapeLayer is added as a sublayer of the view, the border is visible
    [lineView.layer addSublayer:_shapeLayer];
    lineView.layer.cornerRadius = cornerRadius;
    return _shapeLayer;
}

- (UIImage *)cy_getViewImage {
    CGSize size = self.bounds.size;
    CGFloat scale = [UIScreen mainScreen].scale;
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数。
    UIGraphicsBeginImageContextWithOptions(size, YES, scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark - Autoresizing

- (void)cy_setAutoresizeMaskAll {
    self.autoresizingMask =
            UIViewAutoresizingFlexibleBottomMargin |
                    UIViewAutoresizingFlexibleHeight |
                    UIViewAutoresizingFlexibleLeftMargin |
                    UIViewAutoresizingFlexibleRightMargin |
                    UIViewAutoresizingFlexibleTopMargin |
                    UIViewAutoresizingFlexibleWidth;
}

#pragma mark - Layout

- (void)cy_setLayoutEqualConstraint:(CGFloat)constraint {
    [self cy_setLayoutWidthEqualConstraint:constraint];
    [self cy_setLayoutHeightEqualConstraint:constraint];
}

- (void)cy_setLayoutWidthEqualConstraint:(CGFloat)constraint {
    if (@available(iOS 9.0, *)) {
        NSLayoutConstraint *widthConstraint = [self.widthAnchor constraintEqualToConstant:constraint];
        widthConstraint.active = YES;
    }
}

- (void)cy_setLayoutHeightEqualConstraint:(CGFloat)constraint {
    if (@available(iOS 9.0, *)) {
        NSLayoutConstraint *heightConstraint = [self.heightAnchor constraintEqualToConstant:constraint];
        heightConstraint.active = YES;
    }
}
@end

@implementation UIView (ZZViewController)
/**
 view所在的viewController
 */
- (UIViewController *)cy_getCurrentViewInViewController {
    UIResponder *next = [self nextResponder];
    do {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *) next;
        }
        next = [next nextResponder];
    } while (next != nil);
    return nil;
}
@end

@implementation UIView (SSViewSpecialProcess)
- (void)cy_bgViewSetPurpleColorGradient:(CGRect)frame {
    CAGradientLayer *gradient = [CAGradientLayer layer];
    CGRect rect = CGRectMake(0, 0, frame.size.width, frame.size.height);
    gradient.frame = rect;
    gradient.colors = [NSArray arrayWithObjects:
            (id) RGBA_COLOR(164, 218, 236, 1).CGColor,
            (id) RGBA_COLOR(173, 221, 238, 1).CGColor,
            (id) RGBA_COLOR(143, 210, 233, 1).CGColor,
            (id) RGBA_COLOR(148, 212, 234, 1).CGColor, nil];
    gradient.locations = [NSArray arrayWithObjects:
            [NSNumber numberWithFloat:0.01],
            [NSNumber numberWithFloat:0.2],
            [NSNumber numberWithFloat:0.4],
            [NSNumber numberWithFloat:0.9], nil];
    [self.layer addSublayer:gradient];
}
@end
